import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Res,
  Req,
  Headers,
  Session,
  Query,
} from '@nestjs/common';
import { UserService } from './user.service';
import {
  CompareDateDto,
  CreateComponyUserDto,
  CreateUserDto,
  QueryInfo,
  QueryInfoCompanyUser,
  RenewCompany,
  UpdateCompanyDto,
} from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Response } from 'express';
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
const cookie = require('cookie');
@ApiTags('管理员')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}
  @Post()
  @ApiOperation({
    summary: '创建公司和公司员工账号',
  })
  async create(
    @Body() createUserDto: CreateUserDto,
    @Headers('cookie') header,
  ) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.createAccount(createUserDto, token);
    return res;
  }

  @Get()
  @ApiOperation({
    summary: '获取公司员工账号列表',
  })
  @ApiQuery({
    name: 'companyId',
    description: '公司id',
  })
  async findAll(@Headers('cookie') header, @Query('companyId') companyId) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.findAll(companyId, token);
    return res;
  }

  @Post('getAllCompanies')
  @ApiOperation({
    summary: '获取所有公司',
  })
  async getAllCompanies(@Body() query: QueryInfo, @Headers('cookie') header) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.getAllCompanies(query, token);
    return res;
  }

  @Post('checkCompanyValid')
  @ApiOperation({
    summary: '查看公司是否在有效期',
  })
  async checkCompanyValid(
    @Body() company: CompareDateDto,
    @Headers('cookie') header,
  ) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.checkCompanyValid(
      company.companyId,
      token,
    );
    return res;
  }

  @Post('renewCompany')
  @ApiOperation({
    summary: '续费公司',
  })
  async renewCompany(
    @Body() renewCompany: RenewCompany,
    @Headers('cookie') header,
  ) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.renewCompany(
      renewCompany?.companyId,
      token,
      renewCompany?.time,
    );
    return res;
  }

  @Get('freezeCompany')
  @ApiOperation({
    summary: '冻结公司账号',
  })
  @ApiQuery({
    name: 'companyId',
    description: '公司id',
  })
  async getUserInfo(@Query('companyId') companyId, @Headers('cookie') header) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.freezeCompany(companyId, token);
    return res;
  }

  @Post('findAllInCompanyPerson')
  @ApiOperation({
    summary: '获取公司所有员工',
  })
  async findAllInCompanyPerson(
    @Body() query: QueryInfoCompanyUser,
    @Headers('cookie') header,
  ) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.findAllInCompany(query, token);
    return res;
  }

  @Post('createCompanyUser')
  @ApiOperation({
    summary: '录入公司员工信息',
  })
  async createCompanyUser(
    @Body() createComponyUserDto: CreateComponyUserDto,
    @Headers('cookie') header,
  ) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.create(createComponyUserDto, token);
    return res;
  }

  @Post('updateCompanyInfo')
  @ApiOperation({
    summary: '更新公司信息',
  })
  async updateCompanyInfo(
    @Body() updateUserDto: UpdateCompanyDto,
    @Headers('cookie') header,
  ) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.updateCompanyInfo(updateUserDto, token);
    return res;
  }

  @Get('/getCurrentUser')
  @ApiOperation({
    summary: '获取当前登录用户信息',
  })
  async getCurrentUser(@Headers('cookie') header) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.getCurrentUser(token);
    return res;
  }

  @Get('getOneCompanyPersonalInfo')
  @ApiOperation({
    summary: '获取单个用户信息',
  })
  @ApiQuery({
    name: 'id',
    description: '员工id',
  })
  async getOneCompanyPersonalInfo(
    @Query() id: string,
    @Headers('cookie') header,
  ) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.findOne(id);
    return res;
  }

  @Post('getOneCompanyInfo')
  @ApiOperation({ summary: '获取单个公司信息' })
  @ApiQuery({
    name: 'id',
    description: '公司id',
  })
  async getOneCompanyInfo(@Query('id') id: string, @Headers('cookie') header) {
    const cookies = cookie.parse(header);
    const token = cookies?.auth_token;
    if (!token) {
      return {
        code: 401,
        message: '请先登录',
        data: null,
      };
    }
    const res = await this.userService.getOneCompanyInfo(id, token);
    return res;
  }
}
